﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlServerCe;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using RFMobile.Database;

namespace RFMobile
{   
    public partial class machineScreen : Form
    {
        DatabaseManager db;
        String owner;
        String brand;
        String model;
        String code;

        Int32 selected_comtec_id_machine;

        public machineScreen()
        {
            InitializeComponent();
            owner = "";
            brand = "";
            model = "";
            code = "";
            selected_comtec_id_machine = -1;
            this.ShowDialog();
        }

        private void machineScreen_Load(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            db = new DatabaseManager();
            if (db.Query("SELECT company_name FROM owner"))
            {
                SqlCeDataReader dr = db.Command.ExecuteReader();
                while (dr.Read())
                {
                    ownerComboBox.Items.Add(dr["company_name"]);
                }
                dr = null;
            }
            db = null;
            Cursor.Current = Cursors.Default;
        }

        private void menuItem3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void menuItem4_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            if (selected_comtec_id_machine != -1)
            {
                optionMachines op = new optionMachines(selected_comtec_id_machine);
                statusBar1.Text = "";
                op = null;
            }
        }

        private void ownerComboBox_SelectedValueChanged(object sender, EventArgs e)
        {
            statusBar1.Text = "Seleccione una Marca...";
            brandComboBox.Items.Clear();
            modelComboBox.Items.Clear();
            codeComboBox.Items.Clear();
            menuItem1.Enabled = false;
 
            // Cargar marcas
            owner = ownerComboBox.SelectedItem.ToString();
            Cursor.Current = Cursors.WaitCursor;
            db = new DatabaseManager();
            string query_str = "SELECT m.brand FROM machines AS m INNER JOIN owner AS o ON m.comtec_id_owner = o.comtec_id_owner WHERE o.company_name = '" + owner + "'";
            if (db.Query(query_str))
            {
                SqlCeDataReader dr = db.Command.ExecuteReader();
                while (dr.Read())
                {
                    brandComboBox.Items.Add(dr["brand"]);
                }
                dr = null;
            }
            db = null;
            Cursor.Current = Cursors.Default;
        }

        private void brandComboBox_SelectedValueChanged(object sender, EventArgs e)
        {
            statusBar1.Text = "Seleccione un Modelo...";
            modelComboBox.Items.Clear();
            codeComboBox.Items.Clear();
            menuItem1.Enabled = false;

            // Cargar Modelos
            brand = brandComboBox.SelectedItem.ToString();
            Cursor.Current = Cursors.WaitCursor;
            db = new DatabaseManager();
            string query_str = "SELECT model FROM machines WHERE brand = '" + brand + "'";
            if (db.Query(query_str))
            {
                SqlCeDataReader dr = db.Command.ExecuteReader();
                while(dr.Read())
                {
                    modelComboBox.Items.Add(dr["model"]);
                }
                dr = null;
            }
            db = null;
            Cursor.Current = Cursors.Default;
        }

        private void modelComboBox_SelectedValueChanged(object sender, EventArgs e)
        {
            statusBar1.Text = "Seleccione el Código...";
            codeComboBox.Items.Clear();
            menuItem1.Enabled = false;

            // Cargar Códigos
            model = modelComboBox.SelectedItem.ToString();
            Cursor.Current = Cursors.WaitCursor;
            db = new DatabaseManager();
            string query_str = "SELECT cod_mainteiner FROM machines WHERE model = '" + model + "' AND brand ='" + brand + "'";
            if (db.Query(query_str))
            {
                SqlCeDataReader dr = db.Command.ExecuteReader();
                while (dr.Read())
                {
                    codeComboBox.Items.Add(dr["cod_mainteiner"]);
                }
                dr = null;
            }
            db = null;
            Cursor.Current = Cursors.Default;
        }

        private void codeComboBox_SelectedValueChanged(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            code = codeComboBox.SelectedItem.ToString();
            db = new DatabaseManager();
            string query_str = "SELECT comtec_id_machine FROM machines WHERE cod_mainteiner = '" + code + "' AND model ='" + model + "' AND brand = '" + brand + "'";
            if (db.Query(query_str))
            {
                SqlCeDataReader dr = db.Command.ExecuteReader();
                if (dr.Read())
                {
                    selected_comtec_id_machine = Convert.ToInt32(dr["comtec_id_machine"]);
                }
                dr = null;
            }
            db = null;

            if (selected_comtec_id_machine != -1)
            {
                statusBar1.Text = "Avance a las opciones con Siguiente.";
                menuItem1.Enabled = true;
            }
            Cursor.Current = Cursors.Default;
        }
    }
}

